<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>应急调查采集信息</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" href="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="../lib/jquery.mobile-1.4.5/jquery.min.js"></script>
    <script src="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script src="../lib/config.js"></script>
    <script src="../lib/common.js"></script>
    <style>
    header {
        background-color: rgb(90, 153, 222);
        border-bottom: 1px solid #f2f2f2;
    }

    .ui-field-contain {
        margin: 5px 10px;
    }

    table {
        width: 100%;
        border-spacing: 0;
    }

    th {
        text-align: left;
    }

    th h3 {
        margin: .6em 0 .6em .5em;
    }

    td .ui-btn {
        margin: .4em 0 .5em 0;
    }

    td .ui-btn-inner {
        padding: .4em 15px;
    }
    </style>
</head>

<body>
    <div style="margin:10px 10px;">
        <div class="ui-field-contain" style="">
            <label>调查点名称</label>
            <select name="dcdName" id="dcdName"></select>
        </div>
        <div class="ui-field-contain">
            <label>灾害点名称</label>
            <input name="hazardName" id="hazardName" type="text" readonly="readonly">
        </div>
        <div class="ui-field-contain">
            <label>灾害类型</label>
            <input name="hazardType" id="hazardType" type="text" readonly="readonly">
        </div>
        <div class="ui-field-contain">
            <label>发生日期</label>
            <input id="tbDate" type="text" placeholder="日期" readonly="readonly" style="width:120px;">
        </div>
        <div class="ui-field-contain">
            <label>发生时间</label>
            <input id="tbTime" type="text" placeholder="时间" readonly="readonly" style="width:120px;">
        </div>
        <div class="ui-field-contain">
            <label>地理位置</label>
            <input name="YJDC01030" id="YJDC01030" type="text">
        </div>
        <div class="ui-field-contain">
            <label>死亡人数</label>
            <input name="YJDC01180" id="YJDC01180" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>失踪人数</label>
            <input name="YJDC01181" id="YJDC01181" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>受伤人数</label>
            <input name="YJDC01182" id="YJDC01182" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>威胁人数</label>
            <input name="YJDC01190" id="YJDC01190" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>直接损失</label>
            <input name="YJDC01170" id="YJDC01170" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>潜在损失</label>
            <input name="YJDC01200" id="YJDC01200" type="tel">
        </div>
        <div class="ui-field-contain">
            <label>社会危害</label>
            <input name="YJDC01185" id="YJDC01185" type="text">
        </div>
        <div class="ui-field-contain">
            <label>灾害特征</label>
            <input name="YJDC01280" id="YJDC01280" type="text">
        </div>
        <div class="ui-field-contain">
            <label>稳定状态</label>
            <input name="YJDC01150" id="YJDC01150" type="text">
        </div>
        <div class="ui-field-contain">
            <label>发展趋势</label>
            <input name="YJDC01160" id="YJDC01160" type="text">
        </div>
        <div class="ui-field-contain">
            <label>监测建议</label>
            <input name="YJDC01210" id="YJDC01210" type="text">
        </div>
        <div class="ui-field-contain">
            <label>防治建议</label>
            <input name="YJDC01220" id="YJDC01220" type="text">
        </div>
        <div class="ui-field-contain">
            <label>调查人</label>
            <input name="YJDC01250" id="YJDC01250" type="text">
        </div>
        <div class="ui-field-contain">
            <label>调查单位</label>
            <input name="YJDC01260" id="YJDC01260" type="text">
        </div>
        <div class="ui-field-contain">
            <label for="YJDC01270">调查时间</label>
            <input name="YJDC01270" id="YJDC01270" type="text">
        </div>
    </div>
    <div style="margin: 20px 15px;">
        <div style="width:80px;float:left;margin:2px 0px;">
            <label>上传照片</label>
        </div>
        <div style="float:left;margin:0px 10px;">
            <a id="btnUploadImage"> <img src="../image/picture_add.png" style="width:32px;height: 32px;"/> </a>
        </div>
        <div style="float:left;margin:0px 10px;">
            <a id="btnCamera"> <img src="../image/camera_add.png" style="width:32px;height: 32px;"/> </a>
        </div>
        <div style="float:right;margin:0px 10px;">
            <a id="btnSave"> <img src="../image/report_save.png" id="btnCamera"  style="height: 32px;"/></a>
        </div>
        <div style="clear: both;"></div>
        <div id="divImages" style="margin:10px 10px;"></div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
var pics = [];
var dcrwInfo = {};
var rwRecords = [];
apiready = function() {
    $("#btnSave").click(function() {
        showSaveProgress();
        saveRecord();
        api.hideProgress();
    });
    $("#tbDate").val(getLocaleDate());
    $("#YJDC01270").val(getLocaleDatetime());
    $("#YJDC01260").val('湖北省地质环境总站');//dw
    $("#YJDC01250").val('汤亚强');//ren
//  $("#YJDC01260").val(getCookie("first_name"));//dw
//  $("#YJDC01250").val(getCookie("display_name"));//ren
    initialCamera();
    bandDatetimeSelEvent();
    initialDCRW2();
    //获取地理位置
    setInterval(reportLocation, 60000);
};

function initialDCRW2() {
    $.ajax({
            url: ip_req + 'ZHFZ/ZHFZService.svc/GetListYJCZRW01',
            type: 'get',
            dataType: 'jsonp',
            data: { userCode: '4200000000' },
        })
        .done(function(response) {
            if (response != null && response.length > 0) {
                rwRecords = response;
                for (var i = 0; i < rwRecords.length; i++) {
                    var item = "<option value='" + rwRecords[i].YJDCRW01040 + "'>" + rwRecords[i].YJDCRW01020 + "</option>";
                    $("#dcdName").append(item);
                }
                $('#dcdName')[0].selectedIndex = -1;
            }
        });
    $('#dcdName').change(function() {
        var code = $(this).children('option:selected').val();
        for (var i = 0; i < rwRecords.length; i++) {
            if (rwRecords[i].YJDCRW01040 != code)
                continue;
            dcrwInfo = rwRecords[i];
            $("#hazardName").val(dcrwInfo.YJDCRW01060);
            $("#YJDC01030").val(dcrwInfo.YJDCRW01070);
            $("#hazardType").val(dcrwInfo.YJDCRW01050);
        }
        console.log(code);
    })

}

function initialDCRW() {
    var filter = "(YJDCRW01110='A' or YJDCRW01110='B') and YJDCRW01030 >= dateadd(minute,-10080,CONVERT(varchar(100),GETDATE(),20))";
    getRecordsByFilter('qcqf', 'YJDCRW01', filter, function(response) {
        if (response != null && response.record != null && response.record.length > 0) {
            rwRecords = response.record;
            for (var i = 0; i < rwRecords.length; i++) {
                var item = "<option value='" + rwRecords[i].YJDCRW01040 + "'>" + rwRecords[i].YJDCRW01020 + "</option>";
                $("#dcdName").append(item);
            }
            $('#dcdName')[0].selectedIndex = -1;
        }
    }, showErro, true);
    $('#dcdName').change(function() {
        var code = $(this).children('option:selected').val();
        for (var i = 0; i < rwRecords.length; i++) {
            if (rwRecords[i].YJDCRW01040 != code)
                continue;
            dcrwInfo = rwRecords[i];
            $("#hazardName").val(dcrwInfo.YJDCRW01060);
            $("#YJDC01030").val(dcrwInfo.YJDCRW01070);
            $("#hazardType").val(dcrwInfo.YJDCRW01050);
        }
        console.log(code);
    })
}

//汇报应急调查人员位置
function reportLocation() {
    if (!dcrwInfo.YJDCRW01010)
        return;
    console.log('获取设备所处位置');
    //获取设备所处位置
    var baiduLocation = api.require('baiduLocation');
    baiduLocation.startLocation({
        accuracy: '100m',
        filter: 1,
        autoStop: true
    }, function(ret, err) {
        var sta = ret.status;
        if (sta) {
            var str = '经度：' + ret.longitude + '<br>';
            str += '纬度：' + ret.latitude + '<br>';
            str += '更新时间：' + ret.timestamp + '<br>';
            console.log(str);
            saveLocation(ret.longitude, ret.latitude);
        } else
            console.log(err.msg);
    });
}

function saveLocation(lon, lat) {
    var item = {
        "YJDCRYWD01010": Guid(),
        "YJDCRYWD01020": dcrwInfo.YJDCRW01010,
        "YJDCRYWD01040": lon,
        "YJDCRYWD01050": lat,
        "YJDCRYWD01060": getCookie("email"),
        "YJDCRYWD01070": getCookie("display_name"),
        "YJDCRYWD01080": getCookie("first_name"),
        "YJDCRYWD01090": '应急调查人员位置'
    };
    var data = {
        "record": [item]
    };
    addRecords("qcqf", "YJDCRYWD01", data, function(response) {
        console.log("应急调查人员位置上报成功！");
    }, showErro, false);
}

function saveRecord() {
    var getguid = function(id) {
        var item = {
            "Id": id,
            "YJDC01000": dcrwInfo.YJDCRW01040,
            "YJDC01001": dcrwInfo.YJDCRW01160,
            "YJDC01010": $("#tbDate").val() + " " + $("#tbTime").val(),
            "YJDC01020": dcrwInfo.YJDCRW01040.substr(12, 2),
            // "YJDC01020": dcrwInfo.YJDCRW01040.substr(6, 2),
            "YJDC01030": $("#YJDC01030").val(),
            "YJDC01150": $("#YJDC01150").val(),
            "YJDC01160": $("#YJDC01160").val(),
            "YJDC01170": $("#YJDC01170").val(),
            "YJDC01180": $("#YJDC01180").val(),
            "YJDC01181": $("#YJDC01181").val(),
            "YJDC01182": $("#YJDC01182").val(),
            "YJDC01185": $("#YJDC01185").val(),
            "YJDC01190": $("#YJDC01190").val(),
            "YJDC01200": $("#YJDC01200").val(),
            "YJDC01210": $("#YJDC01210").val(),
            "YJDC01220": $("#YJDC01220").val(),
            "YJDC01250": $("#YJDC01250").val(),
            "YJDC01260": $("#YJDC01260").val(),
            "YJDC01270": $("#YJDC01270").val(),
            "YJDC01280": $("#YJDC01280").val()
        };
        item.pics = pics;
        item.pntName = dcrwInfo.YJDCRW01020;
        item.hazardName = dcrwInfo.YJDCRW01060;
        item.hazardType = dcrwInfo.YJDCRW01050;
        save2File(item);
    }
    Guid2(getguid);
}

function save2File(item) {
    api.readFile({
        path: 'fs://dataGather.txt'
    }, function(ret, err) {
        var data = {
            "record": []
        };
        if (ret.status) {
            //先读取以保存的数据
            var preData = JSON.parse(ret.data);
            if (preData != null && preData != '')
                data = preData;
        }
        //写入合并后的数据
        data.record.push(item);
        console.log(JSON.stringify(data));
        api.writeFile({
            path: 'fs://dataGather.txt',
            data: JSON.stringify(data)
        }, function(ret, err) {
            var status = ret.status;
            if (status) {
                showMsg('保存成功！');
            } else {
                var msg = '保存出错: ' + err.code + '错误信息' + err.msg;
                showErro(mssg);
            }
        });
    });
}

function bandDatetimeSelEvent() {
    $("#tbDate").mousedown(function() {
        api.openWin({
            name: 'DateSelector',
            url: '../html/DateSelector.html'
        });
    });
    $("#tbTime").mousedown(function() {
        api.openWin({
            name: 'TimeSelector',
            url: '../html/TimeSelector.html'
        });
    });
    api.addEventListener({
        name: 'DateInputed'
    }, function(ret) {
        if (ret && ret.value) {
            var value = ret.value;
            $("#tbDate").val(value.key1);
        }
    });
    api.addEventListener({
        name: 'timeInputed'
    }, function(ret) {
        if (ret && ret.value) {
            var value = ret.value;
            $("#tbTime").val(value.key1);
        }
    });
}

function onPicBack(ret, err) {
    if (ret) {
        var img = "<img src='" + ret.data + "' style='width:128px;margin:10px 10px;'/>"
        $("#divImages").append(img);
        pics.push(ret.data);
    } else {
        api.alert({
            msg: err.msg
        });
    }
}

function initialCamera() {
    $("#btnUploadImage").click(function() {
        api.getPicture({
            sourceType: 'library',
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: false,
            quality: 50,
            targetWidth: 800,
            targetHeight: 800,
            saveToPhotoAlbum: false
        }, function(ret, err) {
            onPicBack(ret, err);
        });
    });
    $("#btnCamera").click(function() {
        api.getPicture({
            sourceType: 'camera',
            encodingType: 'jpg',
            mediaValue: 'pic'
        }, function(ret, err) {
            onPicBack(ret, err);
        });
    });
}
</script>

</html>